function build_town()



	for y = 1, #town_layout do
		for x = 1, #town_layout do
			if town_layout[y][x] ~= nil then
				if town_layout[y][x] == wall_tile then

					--increase_by = tablelength(town_layout)/grid_size

					Tile = { x = x*tile_size,
						y = y*tile_size,
						image = wood_wall_img, visible = true,
						type = "wall"
						}
					table.insert(maps.town.tiles,Tile)
				elseif town_layout[y][x] == sidewalk_tile then
					Tile = { x = x*tile_size,
						y = y*tile_size,
						image = sidewalk_img, visible = true,
						type = "sidewalk"
						}
					table.insert(maps.town.tiles,Tile)
				elseif town_layout[y][x] == road_tile then
					Tile = { x = x*tile_size,
						y = y*tile_size,
						image = road_img, visible = true,
						type = "road"
						}
					table.insert(maps.town.tiles,Tile)
				elseif town_layout[y][x] == floor_tile then
					Tile = { x = x*tile_size,
						y = y*tile_size,
						image = env_sheet_quads[2], visible = true,
						type = "floor"
						}
					table.insert(maps.town.tiles,Tile)
				elseif town_layout[y][x] == table_tile then
					Tile = { x = x*tile_size,
						y = y*tile_size,
						image = wood_img, visible = true,
						type = "table"
						}
					table.insert(maps.town.tiles,Tile)

				elseif town_layout[y][x] == meta_tile then
					print("in town_layout == meta_tile .. shouldn't be here")
					nt = #threads+1
					--[[
					if nt > 1 then

						threads[nt-1]:wait()
						Tile.image = threads[nt-1]:get('image')
						Tile.name = threads[nt-1]:get('name')
						Tile.x = threads[nt-1]:get('x')
						Tile.y = threads[nt-1]:get('y')
						Tile.visible = threads[nt-1]:get('visible')
						Tile.type = threads[nt-1]:get('type')
						Tile.meta_id = threads[nt-1]:get('meta_id')
						table.insert(maps.town.meta_tiles, Tile.meta_id, Tile)
						table.insert(maps.town.tiles,Tile)

					end
					]]
					threads[nt] = love.thread.newThread(nt.."",'narrative_thread.lua')
					nt = #threads
					threads[nt]:start()
					threads[nt]:set('x', x*tile_size)
					threads[nt]:set('y', y*tile_size)
					threads[nt]:set('visible', true)
					threads[nt]:set('type', "meta")
					threads[nt]:set('meta_id', nt)



					Tile = { x = x*tile_size,
						y = y*tile_size, visible = true,
						type = "meta", meta_id = nt
						}
					threads[nt]:wait()



					Tile.image = threads[nt]:get('image')
					Tile.name = threads[nt]:get('name')

					table.insert(maps.town.meta_tiles, Tile.meta_id, Tile)
					table.insert(maps.town.tiles,Tile)





				else

					Tile = { x = x*tile_size,
						y = y *tile_size,
						image = green_img, visible = true,
						type ="grass"
						}

					table.insert(maps.town.tiles,Tile)

				end

				--print(table.tostring(Tile))

			end
		end
	end


	--assemble_threaded_tiles()
end




function build_map(grid)

	local mapped = false
	for x = 1, grid_size do
		grid[x] = {}
		for y = 1, grid_size do
			grid[x][y] = {}

			--grid[x][y].x = x*tile_size

			tile = RNG:random(100)
			if tile < 45 then

				grid[x][y].type ="wall"


			else
				grid[x][y].type = "floor"

			end

		end

	end
	mapped = true
	--love.filesystem.write("grid.dt", table.show(grid, "loaded_grid"))
	print("map built")

	return mapped

end

function refine_map(grid)

	sur_tiles = 0
	iter = 4
	iter2 = 3
	for i = 1, iter do
		for x = 1, grid_size do
			for y = 1, grid_size do
				sur_tiles = select_surround(1, "wall", x, y,"a", grid)
				sur_tile2 = select_surround(2, "wall", x, y,"a", grid)

				if sur_tiles > 5 then

					grid[x][y].type = "wall"

				end

				if sur_tile2 == 0 then
					grid[x][y].type = "floor"
				end



			end
		end
	end


	for i = 1, iter2 do
		for x = 1, grid_size do
			for y = 1, grid_size do
				sur_tiles = select_surround(1, "wall",x,y, "a", grid)

				if sur_tiles > 5 then
					grid[x][y].type = "wall"
				end

			end
		end
	end

	for x = 1, grid_size do
		for y = 1, grid_size do
			sur_tiles = select_surround(1, "wall",x,y, "a", grid)

			if sur_tiles <= 2 then
				grid[x][y].type = "floor"
			end

		end
	end

	for x = 1, grid_size do
		for y = 1, grid_size do
			if x == 1 or y == 1 or x == grid_size or y == grid_size then
				grid[x][y].type = "wall"
			end


		end
	end



end

function harvest_to_tiles(map, tiles)
	--print(table.tostring(map))
	for y = 1, grid_size do
		for x = 1, grid_size do
			if map[y][x] ~= nil then

				if map[y][x].type == "wall" then
					--print("wall_tile")
					Tile = { x = x*tile_size,
						y = y*tile_size,
						image = brown_tree_img, visible = true,
						type = "wall", explored = false, name = "tree"
					}
					table.insert(tiles, Tile)
				elseif map[y][x].type == "floor" then
					--print("floor_tile")
					Tile = { x = x*tile_size,
						y = y*tile_size,
						image = brown_grain_img, visible = true,
						type = "floor", explored = false, name = "ground"
					}
					table.insert(tiles, Tile)
				end


			end
		end
	end
	--print(table.tostring(tiles))

end

function explore_map(tiles, features, enemies, items, corpses)

	for i = 1, #tiles do
		if p_fov(tiles[i].x,tiles[i].y) then
			tiles[i].explored = true

		end
	end

	for i = 1, #features do
		if p_fov(features[i].x,features[i].y) then
			features[i].explored = true
		end

	end

	for i = 1, #enemies do
		if enemies[i] ~= nil then
			if enemies[i].x ~= nil and enemies[i].y ~= nil then
				if p_fov(enemies[i].x,enemies[i].y) then
					enemies[i].explored = true
				end
			end

		end

	end

	for i = 1, #items do
		if items[i] ~= nil then
			if p_fov(items[i].x, items[i].y) then

				items[i].explored = true
			end

		end
	end

	for i = 1, #corpses do

		if corpses[i] ~= nil then
			if p_fov(corpses[i].x, corpses[i].y) then

				corpses[i].explored = true

			end

		end

	end


end

function rebuild_tiles(map, tiles)

	local gridX = 0
	local gridY = 0

	for i = 1, #tiles do

		gridX = tiles[i].x/tile_size
		gridY = tiles[i].y/tile_size

		tiles[i].type = map[gridX][gridY].type


	end


end

function map_distance(x1,y1,x2,y2)

	local x = x1-x2
	local y = y1-y2
	local absX = math.abs(x)
	local absY = math.abs(y)

	return absX, absY


end

function map_distance_t(x1,y1,x2,y2)

	local x = x1-x2
	local y = y1-y2

	return x, y


end

function remove_fog(tiles)
	for i = 1, #tiles do
		tiles[i].explored = true

	end

end

function ready_map()
		place_player(c_player)
end


function draw_map(map)

	load_images()

	for x = 1, grid_size do
		for y = 1, grid_size do
			if map[x][y] ~= nil then
				if map[x][y].type == "wall" then
					love.graphics.draw(green_img, x*tile_size, y*tile_size)
				elseif map[x][y].type == "floor" then
					love.graphics.draw(purple_img, x*tile_size, y*tile_size)
				end
			end
		end
	end


end

function draw_entity(entity)
	love.graphics.draw(may_img, entity.x, entity.y)
end

function select_surround(res, surround_type, tar_x, tar_y, mode, grid)
	resolution = res
	select_size = resolution * grid_size
	size_inc = 1
	total_inc = size_inc * resolution
	total_selected = 0

	if mode == "a" then
		if res == 1 then

			if tar_x+1 < grid_size and tar_y+1 < grid_size then
				if grid[tar_x+1][tar_y+1].type == surround_type then total_selected = total_selected+1 end
				if grid[tar_x][tar_y+1].type == surround_type then total_selected = total_selected+1 end
				if grid[tar_x+1][tar_y].type == surround_type then total_selected = total_selected+1 end
			end

			if tar_x-1 >= 1 and tar_y-1 >= 1 then
				if grid[tar_x-1][tar_y-1].type == surround_type then total_selected = total_selected+1 end
				if grid[tar_x][tar_y-1].type == surround_type then total_selected = total_selected+1 end
				if grid[tar_x-1][tar_y].type == surround_type then total_selected = total_selected+1 end
			end

			if tar_x-1 >= 1 and tar_y-1 >= 1 and tar_x+1 <= grid_size and tar_y+1 <= grid_size then
				if grid[tar_x+1][tar_y-1].type == surround_type then total_selected = total_selected+1 end
				if grid[tar_x-1][tar_y+1].type == surround_type then total_selected = total_selected+1 end
			end
		else

			for j = 1, resolution do
				for x = 1, total_inc do
					for y = 1, total_inc do
						if tar_x+x > 1 and tar_y+y > 1 and
							tar_x+x < grid_size and tar_y+y < grid_size then
							--print(tar_x+x, tar_y+y)
							if grid[tar_x+x][tar_y+y].type == surround_type then
								--print("grid = surround_type")
								total_selected = total_selected + 1
							end
						end

						if tar_x-x > 1 and tar_y-y > 1 and
							tar_x-x < grid_size and tar_y-y < grid_size then
							--print(tar_x+x, tar_y+y)
							if grid[tar_x-x][tar_y-y].type == surround_type then
								--print("grid = surround_type")
								total_selected = total_selected + 1
							end
						end

					end
				end
			end

		end

	elseif mode == "ns" then
		if res == 1 then
			if tar_y-1 > 1 and tar_y+1 < grid_size then
				if grid[tar_x][tar_y+1].type == surround_type then
					total_selected = total_selected+1
				end
				if grid[tar_x][tar_y-1].type == surround_type then
					total_selected = total_selected+1
				end
			end
		else
			-- res higher than 1
		end
	end

	return total_selected
end

function place_player(grid)

	local potX = RNG:random(grid_size)
	local potY = RNG:random(grid_size)

	local sur = select_surround(2, "floor", potX, potY, "a", grid)

	while sur <= 12 do
		potX = RNG:random(grid_size)
		potY = RNG:random(grid_size)

		sur = select_surround(3, "floor", potX, potY, "a", grid)
		print(sur)

	end

	player_startX = potX*tile_size
	player_startY = potY*tile_size

	print("player set at x: ", player_startX, "player set at y: ", player_startY)


end

function server_map_print(grid, offX, offY)

	load_images()

	for x = 1, grid_size do
		for y = 1, grid_size do

			if grid[x][y].type == "wall" then
				love.graphics.draw(green_img, x*tile_size+offX, y*tile_size+offY)
			elseif grid[x][y].type == "floor" then
				love.graphics.draw(purple_img, x*tile_size+offX, y*tile_size+offY)
			end


		end
	end

end
